/**
 * Copyright (C) 2011 GreenSoft S.A.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.com.greensoft.neosystem.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.commons.lang3.StringUtils;

import ar.com.greensoft.neosystem.model.constraint.PersonaFisicaContraints;
import ar.com.greensoft.neosystem.model.references.EstadoCivil;
import ar.com.greensoft.neosystem.model.references.Sexo;

/**
 * Simple JavaBean domain object that represents a Physical Person in the
 * Customer Relationship Management System.  
 *  
 * @author Jorge E. Villaverde
 * @since 04/01/2012
 * @version 1.0.0
 */
@Entity
@Table(name="persona_fisica")
@NamedQueries({
	@NamedQuery(name=PersonaFisica.FIND_BY_CUIT_QUERY_NAME, 
		query="SELECT c FROM PersonaFisica c WHERE c.cuit = :cuit"),
	@NamedQuery(name=PersonaFisica.FIND_BY_TIPO_NRO_DOC_QUERY_NAME, 
		query="SELECT c FROM PersonaFisica c WHERE c.tipoDocumento = :tipoDocumento AND c.numeroDocumento = :numeroDocumento"),
	@NamedQuery(name=PersonaFisica.FIND_BY_NAME_QUERY_NAME, 
		query="SELECT c FROM PersonaFisica c WHERE c.apellido1 = :name"),
	@NamedQuery(name=PersonaFisica.FIND_BY_SEXO_QUERY_NAME, 
		query="SELECT c FROM PersonaFisica c WHERE c.sexo = :sexo"),
})
@PersonaFisicaContraints
public class PersonaFisica extends Persona { 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static final String FIND_BY_CUIT_QUERY_NAME = "PersonaFisica.findByCuit";
	public static final String FIND_BY_TIPO_NRO_DOC_QUERY_NAME = "PersonaFisica.findByTipoNumeroDocumento";
	public static final String FIND_BY_NAME_QUERY_NAME = "PersonaFisica.findByName";
	public static final String FIND_BY_SEXO_QUERY_NAME = "PersonaFisica.findBySexo";
	
	/**
	 * Person's First Name
	 */
	@NotNull
	@Size(min=1, max=50)
	private String primerNombre;

	/**
	 * Person's Middle Name
	 */
	@Size(max=50)
	private String segundoNombre;
	
	/**
	 * Person's Last Name
	 */
	@NotNull
	@Size(min=1, max=50)
	private String apellido1;

	/**
	 * Person's second last name
	 */
	@Size(max=50)
	private String apellido2;

	/**
	 * Date of Birth (DoB) of the person.
	 */
	@Temporal(TemporalType.TIMESTAMP)
    @Column(name = "fecha_nacimiento")
	private Date fechaNacimiento;
	
	@NotNull
	@Enumerated(EnumType.STRING)
	private EstadoCivil estadoCivil;
	
	@NotNull
	@Enumerated(EnumType.STRING)
	private Sexo sexo;

	public PersonaFisica(){
		super();
		setEstadoCivil(EstadoCivil.SOLTERO);
	}
	
	public String getPrimerNombre() {
		return primerNombre;
	}

	public void setPrimerNombre(String primerNombre) {
		this.primerNombre = primerNombre;
	}

	public String getSegundoNombre() {
		return segundoNombre;
	}

	public void setSegundoNombre(String segundoNombre) {
		this.segundoNombre = segundoNombre;
	}

	public String getApellido1() {
		return apellido1;
	}

	public void setApellido1(String apellido1) {
		this.apellido1 = apellido1;
	}

	public String getApellido2() {
		return apellido2;
	}

	public void setApellido2(String apellido2) {
		this.apellido2 = apellido2;
	}

	public Date getFechaNacimiento() {
		return fechaNacimiento;
	}

	public void setFechaNacimiento(Date fechaNacimiento) {
		this.fechaNacimiento = fechaNacimiento;
	}

	public EstadoCivil getEstadoCivil() {
		return estadoCivil;
	}

	public void setEstadoCivil(EstadoCivil estadoCivil) {
		this.estadoCivil = estadoCivil;
	}

	public String getNombreApellido() {
		StringBuilder builder = new StringBuilder();
		
		builder.append(apellido1);
		if(!StringUtils.isEmpty(apellido2)){
			builder.append(" ");
			builder.append(apellido2);
		}
		builder.append(", ");
		
		builder.append(primerNombre);
		if(!StringUtils.isEmpty(segundoNombre)){
			builder.append(" ");
			builder.append(segundoNombre);
		}
		return builder.toString();
	}
	
	public String toString(){
		return getNombreApellido();
	}

	public Sexo getSexo() {
		return sexo;
	}

	public void setSexo(Sexo sexo) {
		this.sexo = sexo;
	}
}
